#!/usr/bin/env node

const http = require('http'),
        log = console.log,
        html = 
`<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>hello</title>
</head>
<body>
  <h1>hello world!</h1>
</body>
</html>`;


http.createServer((req, res) => {
  log(`${req.method} ${req.url} HTTP/${req.httpVersion}`);
  log(req.headers);
  log();
 
  if(req.url === '/'){
    //send html
    //res.setHeader('Content-Type','text/html');
    //res.setHeader('Content-Length',Buffer.byteLength(html));
    res.writeHead(200,{
      'Content-Type':'text/html',
      'Content-Length':Buffer.byteLength(html)
    })
    res.end(html);
  }else{
    //send err msg
    res.statusCode = 404;
    res.end('Resource not found！')
  }
}).listen(8080);
